<template>
  <div>
    <el-dialog
      v-loading="loading"
      ref="dialog"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showModelSel"
      :append-to-body="true"
      title="品牌机种机型选择"
      width="75%"
      @opened="searchHandle"
    >
      <!-- 查询条件 -->
      <el-form ref="searchForm" :inline="true" :model="search" size="mini">
        <el-form-item prop="brandId" label="品牌名称">
          <el-select
            v-model="search.brandId"
            filterable
            placeholder="请选择"
            class="search-input"
            @change="changeBrandSelect(search.brandId)"
          >
            <el-option
              v-for="item in brandList"
              :key="item.BRAND_ID"
              :label="item.BRAND_NAME"
              :value="item.BRAND_ID"
            >{{ item.BRAND_NAME }}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="seriesId" label="机种名称">
          <el-select v-model="search.seriesId" filterable placeholder="请选择" class="search-input">
            <el-option
              v-for="item in seriesList"
              :key="item.SERIES_ID"
              :label="item.SERIES_NAME"
              :value="item.SERIES_ID"
            >{{ item.SERIES_NAME }}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="modelName" label="机型名称">
          <el-input v-model="search.modelName" placeholder="机型名称" class="search-input"/>
        </el-form-item>
        <el-form-item prop="modelCode" label="机型代码">
          <el-input v-model="search.modelCode" placeholder="机型代码" class="search-input"/>
        </el-form-item>
        <div style="text-align:center">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-refresh" @click="resetForm('searchForm')">重置</el-button>
          </el-form-item>
        </div>
      </el-form>
      <div class="tableHead">
        <div class="tabletitle">品牌机种机型信息</div>
      </div>
      <!-- 用户列表 -->
      <BaseTable
        ref="table"
        @chaneg-size="changeSizeHandle"
        @chaneg-page="changePageHandle"
        @current-change="select"
        @selection-change="handleSelectionVehicleChange"
      >
        <el-table-column label="序号" type="index"/>
        <el-table-column v-if="allowAudit" type="selection" width="40"/>
        <el-table-column prop="BRAND_NAME" label="品牌" min-width="150"/>
        <el-table-column prop="SERIES_NAME" label="机种" min-width="150"/>
        <el-table-column prop="MODEL_NAME" label="机型" min-width="150"/>
      </BaseTable>
      <div style="text-align:center">
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="confirmHandle">确 定</el-button>
          <el-button v-show="allowAudit" type="primary" @click="confirmHandleAll">选择 全部</el-button>
        </span>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getModels,
  getBrandList,
  getSeriesListByBrand
} from '@/api/vehicleProduct/model'
import filtercode from '@/components/Checkbox/filtercode'
import BaseTable from '@/components/BaseTable'
import formMixins from '@/mixins/formMixins'
import rSelect from '@/components/Select/Select'
export default {
  name: 'SelectSoNo',
  components: {
    BaseTable,
    rSelect,
    filtercode
  },
  mixins: [formMixins],
  props: {
    showModelSel: Boolean,
    allowAudit: Boolean
  },
  data() {
    return {
      brandList: [],
      seriesList: [],
      loading: false,
      multipleSelection: [],
      search: {
        limit: 10,
        pageNum: 1,
        brand: '',
        series: '',
        model: '',
        modelCode: '',
        package: ''
      },
      seriesdisabled: true,
      modeldisabled: true,
      packagedisabled: true
    }
  },
  watch: {
    showModelSel(val) {
      if (val) {
        this.searchHandle()
      }
    }
  },
  mounted() {
    this.getBrands()
    this.searchHandle()
  },
  methods: {
    // 获取所有品牌--下拉框
    getBrands() {
      getBrandList()
        .then(res => {
          this.brandList = res
          console.log(this.brandList)
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 品牌选择改变--下拉框
    changeBrandSelect(brandId) {
      getSeriesListByBrand(brandId)
        .then(res => {
          this.seriesList = res
        })
        .catch(err => {
          console.log(err)
        })
    },
    searchHandle() {
      this.search.limit = 5
      this.search.pageNum = 1
      const table = this.$refs['table']
      getModels(this.search)
        .then(res => {
          table.complete().filData(res)
        })
        // .catch(err => {
        //   table.complete()
        // })
    },

    cancel() {
      this.search = {}
      this.$emit('closeModelSel', false)
    },
    // 选择
    select(val) {
      if (val == null) {
        this.multipleSelection = []
      } else {
        this.multipleSelection = val
      }
    },
    handleSelectionVehicleChange(val) {
      if (val == null) {
        this.multipleSelection = []
      } else {
        this.multipleSelection = val
      }
    },
    confirmHandle() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择数据!'
        })
      } else {
        this.$emit('getModelSel', this.multipleSelection)
        this.cancel()
      }
    },
    confirmHandleAll() {
      this.loading = true
      this.search.limit = 10000
      getModels(this.search)
        .then(res => {
          this.$emit('getModelSel', res.rows)
          this.loading = false
          this.cancel()
        })
        .catch(err => {
          this.$message({
            type: 'warning',
            message: '网络异常'
          })
        })
    }
  }
}
</script>

<style scoped>
.magnifier {
  float: right;
  position: absolute;
  right: 50px;
  top: 23px;
  cursor: pointer;
}
</style>
